package com.example.administrator.fangbook.utils;


import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.fangbook.R;
import com.example.administrator.fangbook.appliction.HtlApplication;

/**
 * Created by Administrator on 2018/11/9.
 */

public class ToastUtils {
    private static String mMsg;
    private static Toast toast;

    static {
        handler = new Handler(Looper.getMainLooper());
    }

    private static Handler handler;
    private static TextView textView;

    public static void show(String msg) {
        mMsg = msg;
        handler.post(ToastRunnable);
    }

    public static void show(int resId) {
        show(HtlApplication.getApplication().getResources().getString(resId));
    }

    private static Runnable ToastRunnable = new Runnable() {
        @Override
        public void run() {
            if (toast == null) {
                toast = new Toast(HtlApplication.getApplication());
                textView = (TextView) LayoutInflater.from(HtlApplication.getApplication()).inflate(R.layout.wgt_live_toast_layout, null);
                toast.setView(textView);
                toast.setGravity(Gravity.BOTTOM, 0, 200);
            }
            textView.setText(mMsg);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.show();
        }
    };

    public static void cancel() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }

}
